import * as Api from '../../../apis/importresource';
import {
	observable,
	action,
	extendObservable
} from 'mobx';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	message
} from 'antd';
import {
	WeaLocaleProvider,
} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const {
	TableStore
} = WeaTableNew;

export default class ImportCommon {
	sourceStore = null;
	@observable importType = '';
	@observable importDialog = {
		visible: false,
		condition: [],
		form: new WeaForm(),
		title: '',
		loading: true,
		templetName: '',
		selectedValue: 'workcode',
	}

	@observable resultDialog = {
		title: observable.ref(getLabel(82341, '导入结果')),
		errorTip: observable.ref(getLabel(384059, '导入文件存在以下错误，请解决后重新上传！')),
		visible: false,
		index: 1,
		datas: [],
		importStatus: '',
		pId: '',
		interval: '',
		tableStore: new TableStore(),
		errorInfo: [],
		succnum: 0,
		failnum: 0,
		loading: true,
		component: '',
	}

	@observable recordDialog = {
		title: observable.ref(getLabel(24644, '历史导入记录')),
		visible: false,
		tableStore: new TableStore(),
	}

	@observable logDialog = {
		title: observable.ref(getLabel(24835, '导入日志')),
		visible: false,
		tableStore: new TableStore(),
		isPanelShow: false,
		form: new WeaForm(),
		condition: [],
		loading: true,
	}

	@action
	getImportForm = () => {
		this.importDialog.loading = true;
		let params = {
			importType: this.importType
		};
		if (this.importDialog.formAsyncParams) {
			Object.assign(params, {
				...this.importDialog.formAsyncParams
			});
		}
		Api.getImportForm(params).then(rs => {
			const {
				status,
				condition = [],
			} = rs;

			if (status == '1') {
				this.importDialog.condition = condition;
				const copy = [...condition];
				copy.splice(1);
				copy[0].items.splice(2);
				this.importDialog.form.initFormFields(copy);
				this.importDialog.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getImportProcessInfo = () => {
		let params = {
			importType: this.importType,
			index: this.resultDialog.index,
		};
		Api.getImportProcessLog(params).then(rs => {
			const {
				datas,
				index,
				pId = '',
				status
			} = rs;

			this.resultDialog.loading = false;
			if (status == '1') {
				const {
					importStatus,
					interval,
					errorInfo,
					component
				} = this.resultDialog;

				component && component.scrollToLast();

				if (importStatus == 'over' || importStatus == 'error') {
					clearInterval(interval);
					if (!this.hasErrorInfo(errorInfo)) this.getResultInfo();

					this.sourceStore && this.sourceStore.getSearchList();
					return
				}

				if (rs.importStatus == 'over' || rs.importStatus == 'error') {
					this.resultDialog.loading = true;
				}

				this.resultDialog.importStatus = rs.importStatus;

				if (pId) this.resultDialog.pId = pId;

				if (this.resultDialog.datas.length == 0) {
					this.resultDialog.datas = datas;
				} else {
					this.resultDialog.datas = [...this.resultDialog.datas, ...datas]
				}

				this.resultDialog.index = index;
			}

		});
	}

	@action
	getResultInfo = () => {
		let params = {
			pId: this.resultDialog.pId
		};
		this.resultDialog.loading = true;
		Api.getImportResult(params).then(rs => {
			const {
				datas,
				status
			} = rs, {
				succnum,
				failnum,
				sessionkey = ''
			} = datas;

			if (status == '1') {
				this.resultDialog.succnum = succnum;
				this.resultDialog.failnum = failnum;
				sessionkey && this.resultDialog.tableStore.getDatas(sessionkey, 1);
			}
			this.resultDialog.loading = false;
		});
	}

	@action
	getRecordTable = () => {
		let params = {
			importType: this.importType
		};
		Api.getImportHistory(params).then(rs => {
			const {
				status,
				sessionkey,
			} = rs;

			if (status == '1') {
				this.recordDialog.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getLogTable = () => {
		let params = {
			importType: this.importType,
			...this.logDialog.form.getFormParams(),
		};
		if (this.resultDialog.pId) {
			params = { ...params,
				pId: this.resultDialog.pId
			}
		}
		Api.getImportLogDetail(params).then(rs => {
			const {
				status,
				sessionkey
			} = rs;

			if (status == '1') {
				this.logDialog.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	submitImportInfo = () => {
		const params = {
			...this.importDialog.form.getFormParams(),
			importType: this.importType,
			excelfile: this.importDialog.excelFileId,
			operateType: 'add'
		};
		if (this.importDialog.formAsyncParams) {
			Object.assign(params, {
				...this.importDialog.formAsyncParams
			});
		}
		this.setResultInfoDialogVisible(true);
		Api.saveImport(params).then(rs => {
			const {
				status,
				errorInfo
			} = rs;

			if (status == '1') {
				if (this.hasErrorInfo(errorInfo)) {
					this.resultDialog.errorInfo = errorInfo;
					this.resultDialog.importStatus = 'error';
				}
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
		this.resultDialog.interval = setInterval(() => this.getImportProcessInfo(), 2000);
	}

	@action
	getLogSearchPanelCondition = () => {
		this.logDialog.loading = true;
		let params = {
			importType: this.importType
		};
		Api.getImportLogSearchCondition(params).then(rs => {
			const {
				status,
				searchCondition
			} = rs;

			if (status == '1') {
				this.logDialog.condition = searchCondition;
				this.logDialog.form.initFormFields(searchCondition);
			} else {
				message.error(rs.message);
			}
			this.logDialog.loading = false;
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	setExcelFileNo = (id) => {
		extendObservable(this.importDialog, {
			excelFileId: id
		});
	}

	@action
	submitExcel = () => {
		if (!this.importDialog.excelFileId) {
			message.warning(getLabel(385362, '请选择导入模板!'));
			return;
		}
		this.submitImportInfo();
	}

	@action
	queryRecord = () => {
		this.setRecordDialogVisible(true);
	}

	@action
	queryLog = () => {
		this.setLogDialogVisible(true);
	}

	@action
	jumpToImportResult = (id) => {
		this.resultDialog.pId = id;
		this.resultDialog.importStatus = 'over';
		this.setResultInfoDialogVisible(true);
		this.getResultInfo();
	}


	@action
	setImportDialogVisible = (bool) => {
		this.importDialog.visible = bool;
		if (bool) {
			this.getImportForm();
		} else {
			this.importDialogReset();
		}

	}

	//设置标识，标识当前是那个页面在导入
	@action
	setImportPage = (pageName) => {
		this.pageName = pageName;
	}

	@action
	setResultInfoDialogVisible = (bool) => {
		this.resultDialog.visible = bool;
		if (!bool) {
			clearInterval(this.resultDialog.interval);
			this.resetResultDialogStatus();
		}
	}

	@action
	importDialogReset = () => {
		this.importDialog.condition = [];
		this.importDialog.form = new WeaForm();
		this.importDialog.selectedValue = 'workcode';
		this.importDialog.excelFileId = '';
	}

	@action
	resetResultDialogStatus = () => {
		this.resultDialog.index = 1;
		this.resultDialog.datas = [];
		this.resultDialog.importStatus = '';
		this.resultDialog.pId = '';
		this.resultDialog.interval = '';
		this.resultDialog.errorInfo = '';
		this.resultDialog.loading = true;
	}

	@action
	setRecordDialogVisible = (bool) => {
		this.recordDialog.visible = bool;
		bool && this.getRecordTable();
	}

	@action
	setLogDialogVisible = (bool) => {
		this.logDialog.visible = bool;
		bool && this.getLogTable();
		if (!bool) {
			this.logDialog.isPanelShow = false;
			this.logDialog.form = new WeaForm();
			this.logDialog.condition = [];
		}
	}

	@action
	setLogPanelStatus = (bool) => {
		this.logDialog.isPanelShow = bool;
		bool && this.logDialog.condition.length == 0 && this.getLogSearchPanelCondition();
	}

	@action
	searchLogInfo = () => {
		this.getLogTable();
	}

	hasErrorInfo = (errorInfo) => {
		if (errorInfo.length == 0) return false;
		return true
	}

	@action
	setTempletName = (templetName) => {
		this.importDialog.templetName = templetName;
	}

	@action
	setImportDialogTitle = (title) => {
		this.importDialog.title = title;
	}

	@action
	setImportType = (type) => {
		this.importType = type;
	}

	setFormAsyncParams = (params) => {
		this.importDialog.formAsyncParams = params
	}

	@action
	setSelectedValue = (value) => {
		this.importDialog.selectedValue = value;
	}

	@action
	setScoll = (component) => {
		this.resultDialog.component = component;
	}

	setSourceStore = (store) => {
		this.sourceStore = store;
	}

}